﻿using Design.Common.Utils;

namespace SingletonPattern
{
    internal sealed class DoubleLazybonesSingleton
    {
        private static volatile DoubleLazybonesSingleton _instance;
        private const string _name = "DoubleCheck模式";
        private static readonly object _locker = new();

        private DoubleLazybonesSingleton()
        {
            ConsoleExpansion.WriteLine($"{_name} 创建实例:{Guid.NewGuid()}");
        }

        public static DoubleLazybonesSingleton Instance
        {
            get
            {
                ConsoleExpansion.WriteLine($"{_name} 获取实例 is null {_instance == null}");

                if (_instance == null)
                {
                    lock (_locker)
                    {
                        _instance ??= new();
                    }
                }


                return _instance;
            }
        }
    }
}